﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Revit;

namespace SystemDiagramNew.TypeDefinition
{
	public class LineTypeOperate
	{
		public static void SetLineStyleId(Autodesk.Revit.DB.Document doc, ref DetailCurve detailCurve)
		{
			try
			{
				LineTypeOperate.GetLineStyle(doc, detailCurve);
				detailCurve.LineStyle = LineTypeOperate.GetLineStyle(doc, detailCurve);
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
		}

		public static void SetLineStyleId(Autodesk.Revit.DB.Document doc, ref DetailCurveArray detailCurveList)
		{
			try
			{
				if (detailCurveList != null && detailCurveList.Size >= 1)
				{
                    Element lineStyle = LineTypeOperate.GetLineStyle(doc, detailCurveList.get_Item(0));
					if (lineStyle != null)
					{
						for (int i = 0; i < detailCurveList.Size; i++)
						{
                            detailCurveList.get_Item(i).LineStyle = lineStyle;
						}
					}
				}
			}
			catch
			{
			}
		}

		public static Element GetLineStyle(Autodesk.Revit.DB.Document doc, DetailCurve detailCurve)
		{
			Element result;
			try
			{
				ElementId elementId = null;
				string text = "默认线图案";
				if (PipeInfo.LinePatternId != null && doc.GetElement(PipeInfo.LinePatternId) != null)
				{
					text = doc.GetElement(PipeInfo.LinePatternId).Name;
				}
				string text2 = "(0,0,0)";
				if (PipeInfo.LineColor != null)
				{
					text2 = string.Format("({0},{1},{2})", PipeInfo.LineColor.Blue.ToString(), PipeInfo.LineColor.Red.ToString(), PipeInfo.LineColor.Green.ToString());
				}
				string text3 = string.Concat(new string[]
				{
					text,
					"-",
					PipeInfo.LineWeight.ToString(),
					"-",
					text2
				});
				foreach (ElementId elementId2 in detailCurve.GetLineStyleIds())
				{
					Element element = doc.GetElement(elementId2);
					if (element.Name == text3)
					{
						elementId = element.Id;
						break;
					}
				}
				if (elementId == null)
				{
					using (SubTransaction subTransaction = new SubTransaction(doc))
					{
						subTransaction.Start();
						YJKRevitTools.GetLineCategory(doc, text3, PipeInfo.LinePatternId, PipeInfo.LineWeight, PipeInfo.LineColor);
						subTransaction.Commit();
					}
					doc.Regenerate();
					foreach (ElementId elementId3 in detailCurve.GetLineStyleIds())
					{
						Element element2 = doc.GetElement(elementId3);
						if (element2.Name == text3)
						{
							elementId = element2.Id;
							break;
						}
					}
				}
				result = doc.GetElement(elementId);
			}
			catch
			{
				result = null;
			}
			return result;
		}

        public static bool GetPipeLinePatternAndLineWeight(Autodesk.Revit.DB.Document doc, MEPSystem mepSystem, ref ElementId LinePatternId, ref int LineWeight, ref Autodesk.Revit.DB.Color col)
		{
			bool result;
			try
			{
				PipingSystem pipingSystem = mepSystem as PipingSystem;
				PipingSystemType pipingSystemType = doc.GetElement(pipingSystem.GetTypeId()) as PipingSystemType;
				if (pipingSystemType == null)
				{
					result = false;
				}
				else
				{
					LinePatternId = pipingSystemType.LinePatternId;
					LineWeight = pipingSystemType.LineWeight;
					col = pipingSystemType.LineColor;
					if ((LinePatternId != null && doc.GetElement(LinePatternId) != null) || LineWeight > 0 || LineWeight < 17 || col != null)
					{
						result = true;
					}
					else
					{
						result = false;
					}
				}
			}
			catch
			{
				result = false;
			}
			return result;
		}
	}
}
